package com.managerProvider.service.impl;

import com.managerProvider.mapper.ProviderMapper;
import com.managerProvider.service.ProviderService;
import entity.Provider;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商服务impl
 * The type Provider service.
 *
 * @author tapoliuliyue
 * @date 2022/04/23
 */
@Service
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    private ProviderMapper providerMapper;
    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public ResponseMessage selectAllProvider(Provider provider) {
        ResponseMessage msg = new ResponseMessage();
        try {
            List<Provider> providers = providerMapper.selectAllProvider(provider);
            if (providers == null || providers.size() == 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("selectAllProvider result is empty");
                msg.setFlag(false);
                logger.debug("ProviderServiceImpl selectAllProvider params:" + provider + ",result:" + providers);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setFlag(true);
            msg.setData(providers);
            logger.debug("ProviderServiceImpl selectAllProvider params:" + provider + ",result:" + providers);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("selectAllProvider error");
            msg.setFlag(false);
            logger.error("ProviderServiceImpl selectAllProvider params:" + provider + ",result:" + e.getMessage());
            return msg;
        }
    }

    @Override
    public ResponseMessage insertProvider(Provider provider) {
        ResponseMessage msg = new ResponseMessage();
        try {
            int insert = providerMapper.insert(provider);
            if (insert <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("insertProvider failed");
                msg.setFlag(false);
                logger.debug("ProviderServiceImpl insertProvider params:" + provider + ",result:" + insert);
            }
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setFlag(true);
            msg.setData(insert);
            logger.debug("ProviderServiceImpl insertProvider params:" + provider + ",result:" + insert);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("insertProvider error");
            msg.setFlag(false);
            logger.error("ProviderServiceImpl insertProvider params:" + provider + ",result:" + e);
            return msg;
        }
    }

    @Override
    public ResponseMessage updateProvider(Provider provider) {
        ResponseMessage msg = new ResponseMessage();
        try {
            int update = providerMapper.updateById(provider);
            if (update <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("updateProvider failed");
                msg.setFlag(false);
                logger.debug("ProviderServiceImpl updateProvider params:" + provider + ",result:" + update);
            }
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setFlag(true);
            msg.setData(update);
            logger.debug("ProviderServiceImpl updateProvider params:" + provider + ",result:" + update);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("updateProvider error");
            msg.setFlag(false);
            logger.error("ProviderServiceImpl updateProvider params:" + provider + ",result:" + e);
            return msg;
        }
    }
}
